package javasql;

import java.sql.*;
import java.text.MessageFormat;

public class Jdbc6 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
            String url = "jdbc:mysql://localhost:3306/kgb762?useSSL=false";
            Connection connection = null;
            Statement st = null;
            ResultSet rs = null;
            try {
            connection = DriverManager.getConnection(url, "root", "123456");
            System.out.println(connection.getClass().getName());
            System.out.println("链接已经建立");
            st = connection.createStatement();
            System.out.println("上下文:" + st.getClass().getName());
            rs = st.executeQuery("select sex,count(*) as cnt from s_student group by sex;");
            System.out.println(rs.getClass().getName());
            while (rs.next()){
                String sex = rs.getString("sex");
                int count = rs.getInt("cnt");
                System.out.println("sex = " + sex + ",count = " + count);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }try{
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
